import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'dva'
import UserList from './List'
import Modals from './Modal'
import { message,Modal} from 'antd';
const confirm = Modal.confirm;

function User({location, dispatch, customerInforDetail, loading}) {

  const { curItem, modalVisible, modalType, record, selectedRowKeys} = customerInforDetail
  const modalProps = {
    item: modalType === 'create' ? {} : curItem,
    visible: modalVisible,
    maskClosable: false,
    title: `${modalType === 'create' ? '新增客户信息' : '客户信息'}`,
    width: '80%',
    wrapClassName: 'vertical-center-modal',
    onOk (data) {
      //console.log('新增>>>',data);
      dispatch({
        type: `customerInforDetail/${modalType}`,
        payload: data,
      })
    },
    onCancel () {
      dispatch({
        type: 'customerInforDetail/hideModal',
      })
    },
  }
  /**
   * 修改
   * @author 李爽
   * @version [version]
   */
  function onEditItem(item) {

    dispatch({
      type: 'customerInforDetail/showModal',
      payload: {
        modalType: 'update',
        curItem : item
      }
    })
  }

/**
 * 选中修改
 */
 function onEditItem1() {
   if(!record){//未定义状态判断
     message.warning('请选中一条数据！');
     return;
   }else if(record.length == 1){//只有选中一条数据才进行修改
     dispatch({
       type: 'customerInforDetail/showModal',
       payload: {
         modalType: 'update',
         curItem: record[0],
       },
     })
   }else {
     message.warning('请选中一条数据！');
     return;
   }
 }

 /**
  * 删除
  */
 // function onDeleteItem(id) {
 //   dispatch({
 //     type: 'customerInforDetail/deleteBatch',
 //     payload: {id}
 //   })
 //
 // }

 function onDeleteItem(id) {
  dispatch({
    type: 'customerInforDetail/delete',
     payload: {id}
   })
}
function onDeleteBatch(id) {
  dispatch({
    type: 'customerInforDetail/deleteBatch',
    payload: { ids }
  })
}
 /**
  * 批量删除
  */
function onDeleteItem1(id){
  console.log('2222',id);
  if(!id){
    message.warning('请至少选择一条数据');
    return;
  }else if(record.length>0){
    confirm({
      title:'您确定要删除么',
      onOk(){
        dispatch({
          type:'customerInforDetail/delete',
          payload:{id}
        })
      }
    })
  }else{
    message.warning('请至少选中一条数据！');
    return;
  }
}

  function onStatusItem(item) {
    dispatch({
      type: 'customerInforDetail/updateStatus',
      payload: {
        curItem: item
      }
    })
  }

  function onSearch(values){
    dispatch({
      type: 'customerInforDetail/search',
      payload: values
    })
  }

/**
 * 增加
 */
function onCreate(){
  dispatch({
    type:'customerInforDetail/showModal',
    payload:{modalType:'create'}
  })
}

/**
 * 查看客户
 */
function onLock() {
  if(!record){//未定义状态判断
    message.warning('请选中一条数据！');
    return;
  }else if(record.length == 1){
    dispatch({
      type: 'customerInforDetail/showModal',
      payload: {
        modalType: 'lock',
        curItem: record[0],
      },
    })
  }else {
    message.warning('请选中一条数据！');
    return;
  }
}


  const listProps = {
    customerInforDetail,
    loading,
    location,
    onDeleteItem1,
    onDeleteItem,
    onStatusItem,
    onSearch,
    dispatch,
    onCreate,
    onEditItem1,
    onLock

  }

  return (
    <div className='content-inner'>

      <UserList {...listProps}/>
      {modalVisible && <Modals {...modalProps} />}
    </div>
  );
}

User.propTypes = {
  customerInforDetail: PropTypes.object,
  location: PropTypes.object,
  dispatch: PropTypes.func
};

function mapStateToProps({ customerInforDetail }){
  return { customerInforDetail }
}

export default connect(mapStateToProps)(User);
